package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.mapper.AddressBookMapper;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import com.sky.vo.AddressBookVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressBookServiceImpl implements AddressBookService {
    @Autowired
    private AddressBookMapper addressBookMapper;

    /**
     * 新增地址
     *
     * @param addressBookVO
     */
    @Override
    public void save(AddressBookVO addressBookVO) {
        addressBookVO.setUserId(BaseContext.getCurrentId());
        addressBookMapper.insert(addressBookVO);
    }

    /**
     * 查询当前登录用户的所有地址信息
     *
     * @return {@link AddressBookVO}
     */
    @Override
    public List<AddressBookVO> list() {
        Long userId = BaseContext.getCurrentId();
        List<AddressBookVO> addressBookVOList = addressBookMapper.selectByUserId(userId);
        return addressBookVOList;
    }

    /**
     * 查询默认地址
     *
     * @return {@link AddressBookVO}
     */
    @Override
    public AddressBookVO defaultAddress() {
        Long userId = BaseContext.getCurrentId();
        AddressBookVO addressBookVO = addressBookMapper.selectDefaultByUserId(userId);
        return addressBookVO;
    }

    /**
     * 根据id查询地址
     *
     * @param id
     * @return {@link AddressBookVO}
     */
    @Override
    public AddressBookVO selectAddressById(Long id) {
        AddressBookVO addressBookVO = addressBookMapper.selectAddressById(id);
        return addressBookVO;
    }

    /**
     * 根据id修改地址
     *
     * @param addressBookVO
     */
    @Override
    public void update(AddressBookVO addressBookVO) {
        addressBookMapper.update(addressBookVO);
    }

    /**
     * 根据id删除地址
     *
     * @param id
     */
    @Override
    public void delete(Long id) {
        addressBookMapper.delete(id);
    }

    /**
     * 设置默认地址
     *
     * @param addressBookVO
     */
    @Override
    public void setDefaultAddress(AddressBookVO addressBookVO) {
        addressBookVO.setUserId(BaseContext.getCurrentId());
        addressBookVO.setIsDefault(0);
        addressBookMapper.updateDefaultAddress(addressBookVO);
        addressBookVO.setIsDefault(1);
        addressBookMapper.update(addressBookVO);
    }


}
